package com.wangjiang.foundation.common.rest;


import com.wangjiang.foundation.common.rest.entity.Result;
import com.wangjiang.foundation.common.rest.entity.ResultCodeEnum;

public class ResultGenerator {
    private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";
    private static final String DEFAULT_INTERNAL_ERROR_MESSAGE = "INTERNAL_ERROR";

    //200
    public static <T> Result<T> genSuccessResult(T data) {
        return new Result()
                .setCode(ResultCodeEnum.SUCCESS.getCode())
                .setMessage(DEFAULT_SUCCESS_MESSAGE)
                .setData(data);
    }

    //200
    public static <T> Result<T> genSuccessResult(String message,T data) {
        return new Result()
                .setCode(ResultCodeEnum.SUCCESS.getCode())
                .setMessage(message)
                .setData(data);
    }

    // 400
    public static Result genFailResult(String message) {
        return new Result()
                .setCode(ResultCodeEnum.FAIL.getCode())
                .setData("")
                .setMessage(message);
    }

    // 40002
    // 请确保grant_type字段值为client_credential
    public static Result genGrantTypeErrorResult() {
        return new Result()
                .setCode(40002)
                .setData("")
                .setMessage("请确保grant_type字段值为client_credential");
    }

    // 40001
    // AppSecret错误或者AppSecret不属于这个公众号，请开发者确认AppSecret的正确性
    public static Result genAppIdAppSecretErrorResult() {return new Result()
            .setCode(40001)
            .setData("")
            .setMessage("AppSecret错误或者AppSecret不属于这个应用，请开发者确认AppSecret的正确性\n");
    }
    
 // 403
    public static Result genForbidenResult(String message) {
        return new Result()
                .setCode(ResultCodeEnum.FORBIDDEN.getCode())
                .setData("")
                .setMessage(message);
    }

    // 404
    public static Result genNoFoundResult(String message) {
        return new Result()
                .setCode(ResultCodeEnum.NOT_FOUND.getCode())
                .setMessage(message);
    }

    // 500
    public static Result genInternalErrorResult(String message) {
        if(message.equals("")) message = DEFAULT_INTERNAL_ERROR_MESSAGE;

        return new Result()
                .setCode(ResultCodeEnum.INTERNAL_SERVER_ERROR.getCode())
                .setMessage(message);
    }

    // 501
    public static Result gen501Result(String message) {
        if(message.equals("")) message = DEFAULT_INTERNAL_ERROR_MESSAGE;

        return new Result()
                .setCode(ResultCodeEnum.INTERNAL_SERVER_501_ERROR.getCode())
                .setMessage(message);
    }


    // 510
    public static Result genDownMicroserviceResult(String microserviceName) {

        return new Result()
                .setCode(ResultCodeEnum.MICROSERVICE_SERVER_ERROR.getCode())
                .setMessage(microserviceName + " is down");
    }


    //401
    public static Result genUnauthorizedResult(String message) {
        return new Result()
                .setCode(ResultCodeEnum.UNAUTHORIZED.getCode())
                .setMessage(message);
    }

    //401
    public static Result genUnauthorizedResult() {
        return new Result()
                .setCode(ResultCodeEnum.UNAUTHORIZED.getCode())
                .setMessage("token格式不正确，请重新登录");
    }

    //403
    public static Result genForbidden() {
        return new Result()
                .setCode(ResultCodeEnum.FORBIDDEN.getCode())
                .setMessage("无权限");
    }

    //403
    public static Result genForbidden(String message) {
        return new Result()
                .setCode(ResultCodeEnum.FORBIDDEN.getCode())
                .setMessage(message);
    }

    //402
    public static Result genIllegalTokenResult() {
        return new Result()
                .setCode(ResultCodeEnum.TOKENOUTOFTIME.getCode())
                .setMessage("无效token！");
    }
    // 空指针异常
    public static Result NullPointResult(Exception e) {
        return new Result()
                .setCode(ResultCodeEnum.INTERNAL_SERVER_ERROR.getCode())
                .setMessage("空指针异常")
                .setData(e);
    }


    public static Result genLoginFailResult(String s) {
        return new Result()
                .setCode(ResultCodeEnum.LOGIN_ERROR.getCode())
                .setMessage(s)
                .setData(null);
    }
}
